/**
 * 更新PDM发布状态
 * 
 * @author 魏文杰 
 * @Version 1.0
 * @Date 2013-10-08
 */

package ext.generic.erp.service;

import java.util.List;

import wt.iba.value.IBAHolder;
import wt.part.WTPart;
import wt.util.WTException;
import ext.com.iba.IBAUtil;
import ext.generic.erp.bean.GenericIEBean;
import ext.generic.erp.common.CommonPDMUtil;
import ext.generic.erp.query.QueryBomReleaseStatus;
import ext.generic.erp.query.QueryPartReleaseStatus;
import ext.generic.erp.task.UpdateWriteBackStatusIETask;
import ext.generic.erp.util.BusinessRuleXMLConfigUtil;
import ext.generic.erp.util.IntegrationConstant;
import ext.generic.erp.util.PDMIntegrationLogUtil;

public class UpdatePDMDataService {
	private static String clazz = UpdatePDMDataService.class.getName() ;
	
	private static boolean verbose = PDMIntegrationLogUtil.verbose ;
	
	//错误原因
	private static final String ERPERRORMSG = "erpErrorMsg";
	
	public static void updateERPAcceptStatus(){
		updateERPPartAcceptStatus( BusinessRuleXMLConfigUtil.getInstance().getPartReleaseStatus() ) ;
		
		updateERPBomAcceptStatus( BusinessRuleXMLConfigUtil.getInstance().getBomReleaseStatus() ) ;
	}
	
	public static void updateERPPartAcceptStatus( String ibaName ){
		List<GenericIEBean> list = QueryPartReleaseStatus.query() ;
		
		updateERPAcceptStatus(QueryPartReleaseStatus.getTableName(), ibaName, list);
	}
	
	public static void updateERPBomAcceptStatus( String ibaName ){
		List<GenericIEBean> list = QueryBomReleaseStatus.query() ;
		
		updateERPAcceptStatus(QueryBomReleaseStatus.getTableName(), ibaName, list);
	}

	private static void updateERPAcceptStatus(String table, String ibaName, List<GenericIEBean> list ) {
		int size = list.size() ;

		for(int i = 0 ; i < size  ; i++ ){
			GenericIEBean genericIEBean = list.get(i) ;
			
			if(verbose){
				PDMIntegrationLogUtil.printLog(clazz, genericIEBean.toString() , IntegrationConstant.LOG_LEVEL_NORMAL ) ;
			}
			
			String partNumber = genericIEBean.getAttribute("item_id") ;
			String partView = genericIEBean.getAttribute("pview") ;
			String version = genericIEBean.getAttribute("version") ;
			String flag = genericIEBean.getAttribute("flag") ;
			//填加错误原因
			String erpErrorMsg = genericIEBean.getAttribute("ERP_ERROR_MSG") ;

			if( erpErrorMsg == null ){
				erpErrorMsg = "";
			}
			
			try {
				WTPart part = CommonPDMUtil.getWTPartByNumViewAndVersion( partNumber , partView , version);
				
				if( part != null ){
					if( flag != null && flag.equals( IntegrationConstant.ERP_PROCESS_SUCCESS )){
						IBAUtil.forceSetIBAValue( part , ibaName , IntegrationConstant.ERP_PROCESS_SUCCESS_CN);
					}else if( flag != null && flag.equals( IntegrationConstant.ERP_PROCESS_FAILED ) ){
						IBAUtil.forceSetIBAValue( part , ibaName , IntegrationConstant.ERP_PROCESS_FAILED_CN);
						
						//错误原因更新
						IBAUtil.forceSetIBAValue( part , ERPERRORMSG , erpErrorMsg );
					}
					
					String where = "item_id='" + partNumber + "' " ;
					where = where + " And " + "pview='" + partView + "' " ;
					where = where + " And " + "version='" + version + "' " ;
					
					UpdateWriteBackStatusIETask.update( table , where ) ;					
				}
			} catch (WTException e) {
				e.printStackTrace();
			}
		}
	}
	
	public static void updatePDMSucessful(IBAHolder ibaHolder , String ibaName ){
		updatePDMData( ibaHolder , ibaName , IntegrationConstant.PDM_RELEASE_STATUS_CN ) ;
	}
	
	public static void updatePDMFailed(IBAHolder ibaHolder , String ibaName ){
		updatePDMData( ibaHolder , ibaName , "") ;
	}
	
	public static void updateERPSucessful(IBAHolder ibaHolder , String ibaName ){
		updatePDMData( ibaHolder , ibaName , IntegrationConstant.ERP_PROCESS_SUCCESS_CN ) ;
	}
	
	public static void updateERPFailed(IBAHolder ibaHolder , String ibaName ){
		updatePDMData( ibaHolder , ibaName , IntegrationConstant.ERP_PROCESS_FAILED_CN ) ;
	}
	
	protected static void updatePDMData(IBAHolder ibaHolder , String ibaName , String ibaValue){
		if( ibaHolder != null ){
			try {
				IBAUtil.forceSetIBAValue(ibaHolder, ibaName, ibaValue ) ;
				
			} catch (WTException e) {
				e.printStackTrace();
			}
		}
	}
}
